//
//  BRMInfoTopView.m
//  BrezzeSaaS
//
//  Created by macos on 16.3.22.
//  Copyright © 2022 Brezze GWG. All rights reserved.
//

#import "BRMInfoTopView.h"

@interface BRMInfoTopView ()<QMUITextFieldDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    UIView *_topView;
    UIView *_imageView;
    UIImageView *_logoImg;
    UITextField *_textFiled;
//    UICollectionView *_myCollectionView;
    QMUILabel *_changeL;
}


@end

@implementation BRMInfoTopView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)updateImages:(NSArray *)imgs {
    
//    for (UIView *subView in _imageView.subviews) {
//        subView.hidden = YES;
//    }
    NSString *fileStr = USERManager.filesSever;
    NSInteger count = imgs.count;
//    if (count > 3) count = 3;
//    for (int i = 0; i < count; i++) {
//        UIImageView *img = [_imageView viewWithTag:100 + i];
//        img.hidden = NO;
//        NSString *imgstr = [NSString stringWithFormat:@"%@%@",fileStr,imgs[i]];
//        [img sd_setImageWithURL:[NSURL URLWithString:imgstr] placeholderImage:UIImageMake(@"pic_default")];
//    }
    NSMutableArray *imgMutArr  = [NSMutableArray array];
    for (NSString *patch in imgs) {
        NSString *imgstr = [NSString stringWithFormat:@"%@%@",fileStr,patch];
        [imgMutArr addObject:imgstr];
    }
    self.imgDatas = imgMutArr;
    CGSize imgsize = [self imgSize];
    CGFloat bottom_Y = 40.f;
    NSString *add = Localized(@"shop_add");
    if (count != 0) {
        bottom_Y = 56.f + imgsize.height;
        add = Localized(@"shop_change");
    }
    _changeL.text = add;
    [_topView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-bottom_Y);
    }];
}

- (void)setImgDatas:(NSArray *)imgDatas {
    _imgDatas = imgDatas;
    _myCollectionView.hidden = imgDatas.count == 0;
    [_myCollectionView reloadData];
    
}

- (void)toImgs {
    if (self.imgsBlock) {
        self.imgsBlock();
    }
}

- (CGSize)imgSize {
    CGFloat img_w = (kScreenWidth - 84) / 3.0;
    CGFloat img_h = img_w * 68.0 / 102.0;
    return CGSizeMake(img_w, img_h);
}

- (void)setShopName:(NSString *)shopName {
    _shopName = shopName;
    _textFiled.text = shopName;
}

- (void)setLogoURL:(NSString *)logoURL {
    _logoURL = logoURL;
    NSString *url = [NSString stringWithFormat:@"%@%@",USERManager.filesSever,logoURL];
    [_logoImg sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:UIImageMake(kDefaultImg)];
}

- (void)toLogo {
    if (self.logoBlock) {
        self.logoBlock();
    }
}

#pragma mark - Collection Delegate --
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imgDatas.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPathc {
    
    return [self imgSize];
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    BRStationImgCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BRStationImgCollectionViewCell" forIndexPath:indexPath];
    cell.layer.cornerRadius = 4;
    cell.layer.masksToBounds = YES;
    
    NSURL *url = [NSURL URLWithString:self.imgDatas[indexPath.row]];
    
    [cell.img sd_setImageWithURL:url placeholderImage:UIImageMake(kDefaultImg)];
    
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    BRStationImgCollectionViewCell *cell = (BRStationImgCollectionViewCell *)[collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:indexPath.row inSection:0]];
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickImg:imageView:data:)]) {
        [self.delegate clickImg:indexPath.row imageView:cell.img data:self.imgDatas];
    }
//    [self clickImg:indexPath.row imageView:cell.img];
    
}

#pragma mark - UI --
- (void)setUI {
    self.layer.cornerRadius = 13;
    self.layer.masksToBounds = YES;
    self.backgroundColor = UIColorWhite;
    UIView *topView = [UIView new];
    topView.backgroundColor = UIColorClear;
    [self addSubview:topView];
    _topView = topView;
    
    UIImageView *header = [[UIImageView alloc] initWithImage:UIImageMake(kDefaultImg)];
    header.layer.cornerRadius = 28.;
    header.layer.masksToBounds = YES;
    header.contentMode = UIViewContentModeScaleAspectFill;
    [topView addSubview:header];
    [header mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.width.height.mas_equalTo(56);
        make.left.offset(16);
    }];
    _logoImg = header;
    UIImageView *camera = [[UIImageView alloc] initWithImage:UIImageMake(@"info_camera")];
    [self addSubview:camera];
    [camera mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.height.mas_equalTo(21);
        make.right.bottom.equalTo(header).offset(0);
    }];
    
    UIControl *logoBtn  = [[UIControl alloc] init];
    [logoBtn addTarget:self action:@selector(toLogo) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:logoBtn];
    [logoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.bottom.equalTo(header);
        
    }];
    
    QMUITextField *nameTF = [[QMUITextField alloc] init];
    nameTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    nameTF.clearButtonPositionAdjustment = UIOffsetMake(-20, 0);
//    nameTF.textInsets = UIEdgeInsetsMake(0, 110, 0, 20);
    nameTF.placeholderColor = UIColor.qd_placeholderColor;
    nameTF.minimumFontSize = 10;
    nameTF.adjustsFontSizeToFitWidth = YES;
    nameTF.textColor =  UIColorText1;
    nameTF.tintColor = kColorMain;
    nameTF.font  = [UIFont br_fontM:16];
//    nameTF.placeholder = Localized(@"Login_enterPhone");
    [topView addSubview:nameTF];
//    nameTF.layer.cornerRadius = 30;
//    nameTF.backgroundColor = UIColorMake(241, 245, 251);
    [nameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-16);
        make.height.mas_equalTo(33);
        make.centerY.equalTo(topView);
        make.left.equalTo(header.mas_right).offset(16);
    }];
    nameTF.text = Localized(@"shop_logo");
    nameTF.maximumTextLength = 15;
    nameTF.delegate = self;
//    nameTF.keyboardType = UIKeyboardTypePhonePad;
    _textFiled = nameTF;
    UIView *line = [BRToolsView getLineView];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.left.offset(16);
        make.right.offset(0);
        make.top.equalTo(topView.mas_bottom).offset(0);
    }];
    nameTF.userInteractionEnabled = NO;
    
    UIImageView *topRimg = [[UIImageView alloc] initWithImage:UIImageMake(@"arrow_right_grayLow6")];
    [topView addSubview:topRimg];
    [topRimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(6);
        make.height.mas_equalTo(10);
        make.centerY.offset(0);
        make.right.offset(-16);
    }];
    
    UIImageView *rightImg = [[UIImageView alloc] initWithImage:UIImageMake(@"arrow_right_grayLow6")];
    [self addSubview:rightImg];
    [rightImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(6);
        make.height.mas_equalTo(10);
        make.top.equalTo(topView.mas_bottom).offset(15);
        make.right.offset(-16);
    }];
    QMUILabel *changeL = [BRToolsView br_label:Localized(@"shop_add") textColor:kColorMain font:[UIFont fontM:13]];
    [self addSubview:changeL];
    [changeL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(rightImg).offset(0);
        make.right.equalTo(rightImg.mas_left).offset(-8);
    }];
    _changeL = changeL;
    
    QMUILabel *leftL = [BRToolsView br_label:Localized(@"shop_shopPhoto") textColor:kColorTitle font:[UIFont fontM:14]];
    [self addSubview:leftL];
    [leftL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.centerY.equalTo(rightImg);
    }];
    
    //图片
    UICollectionViewFlowLayout *layout  = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(0, 16, 0, 16);
    UICollectionView *imgCollection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    [self addSubview:imgCollection];
    imgCollection.delegate = self;
    imgCollection.dataSource = self;
    [imgCollection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.top.equalTo(topView.mas_bottom).offset(40);
        make.right.offset(0);
        make.height.mas_equalTo(76);
    }];
    [imgCollection registerNib:[UINib nibWithNibName:NSStringFromClass([BRStationImgCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:@"BRStationImgCollectionViewCell"];
    imgCollection.backgroundColor = UIColor.qd_backgroundColor;
    imgCollection.showsHorizontalScrollIndicator = NO;
    imgCollection.hidden = YES;
    _myCollectionView = imgCollection;
    
    // end
    // 默认bottom 40 有图片 40+16+ img_h
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.mas_equalTo(76);
        make.bottom.offset(-(40));
    }];
    
    UIControl *imgBtn = [[UIControl alloc] init];
    [self addSubview:imgBtn];
    [imgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView.mas_bottom);
        make.left.right.offset(0);
        make.height.mas_equalTo(40);
    }];
    [imgBtn addTarget:self action:@selector(toImgs) forControlEvents:UIControlEventTouchUpInside];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
